package com.mankind.ssm.service;

import com.mankind.ssm.common.CONSTANT;
import com.mankind.ssm.common.Page;
import com.mankind.ssm.mapper.PermissionMapper;
import com.mankind.ssm.mapper.RolePermissionMapper;
import com.mankind.ssm.pojo.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Mankind on 2017/7/21.
 */
@Service("permissionService")
public class PermissionServiceImpl implements  PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    public List<Permission> selectAllPermission() {
        return permissionMapper.selectAll();
    }

    public Integer getTotalNumber() {
        Integer num=permissionMapper.getTotalNumber();
        if(num==0) return 0;
        return (num-1)/ CONSTANT.USERINFO_LIST_SIZE+1;
    }

    public Integer getTotalNumber(String text) {
        int num=permissionMapper.getFuzzyQueryTotalNumber(text);
        System.out.println("num:"+num);
        if(num==0) return 0;
        return (num-1)/ CONSTANT.USERINFO_LIST_SIZE+1;
    }
    public Page<Permission> getRoleByPage(int pageSize, int currentPage) {
        Page<Permission> p=new Page<Permission>();
        int start=(currentPage-1)*pageSize;
        List<Permission> u=permissionMapper.findByPage(start,pageSize);
        p.setNumberPage(pageSize);
        p.setObj(u);
        System.out.println("傻逼url:"+u.get(0).getResourceUrl());
        return p;
    }

    public Page<Permission> fuzzyQueryByPage(int currentPage, int pageSize, String text) {
        Page<Permission> p=new Page<Permission>();
        int start=(currentPage-1)*pageSize;
        List<Permission> u=permissionMapper.fuzzyQueryByPage(start,pageSize,text);
        p.setNumberPage(pageSize);
        p.setObj(u);
        return p;
    }

    public Permission getPermissionById(Integer id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    public void insertPermission(Permission permission) {
        permissionMapper.insert(permission);
    }

    public void deleteByPermissionId(Integer permissionId) {
        rolePermissionMapper.deleteByPermissionId(permissionId);
        permissionMapper.deleteByPrimaryKey(permissionId);
    }
    public void updatePermission(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }
}
